package com.chj.chuji.tijie11;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

//https://blog.csdn.net/xiaoweiba_h/article/details/81836804
public class CheckBST {

	public static class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;

		// 构造函数
		public TreeNode() {
		}

		public TreeNode(int data) {
			this.val = data;
		}
	}

	public boolean isBST(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		TreeNode p = root, pre = null;
		while (p != null || !stack.isEmpty()) {
			while (p != null) {
				stack.push(p);
				p = p.left;
			}
			TreeNode t = stack.pop();
			if (pre != null && t.val <= pre.val)
				return false;
			pre = t;
			p = t.right;
		}
		return true;
	}

	public static List<Integer> list = new ArrayList<>();

	public static void isTree(TreeNode root) {
		if (root == null)
			return;
		isTree(root.left);
		list.add(root.val);
		isTree(root.right);
	}

	public static void main(String[] args) {
		// 假设给定了树的根节点root
		TreeNode root = new TreeNode(0);
		isTree(root);
		boolean ist = true;
		for (int i = 1; i < list.size(); i++) {
			if (list.get(i - 1) >= list.get(i)) {
				ist = false;
			}
		}
		System.out.println(ist);
	}
}
